package appfuse.framework.util;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import appfuse.framework.database.SqlCommand;
import appfuse.framework.exceptions.NoTableException;
/**
 * <p>Title: 通过Id获取其它需要的信息</p>
 * <p>Description: 通用</p>
 * <p>Copyright: YeGoal</p>
 * <p>Company: YeGoal</p>
 * @author Owen
 * @version 1.0
 */
public class IdtoInfo{
	private Log log = LogFactory.getLog(this.getClass());
	
	/**
	 * 根据Id找名字
	 * 参数
	 * String oTable 表
	 * Integer oId 具体的Id
	 * String oName 根据id找的某个字段
	 */
    public String IdtoName(String oTable, Integer oId, String oName) throws NoTableException {
		//增加球员信息
		SqlCommand sqlCommand;
		String sql = "select " + oName + " from " + oTable + " where " + "id = '" + oId + "'";
		sqlCommand = new SqlCommand(sql);
		try {
			ResultSet rs = sqlCommand.executeQuery(); //执行
			
			if(rs.next()){ //如果找到
				return rs.getString(oName);
			}
			
		} catch (SQLException e) {
			try {
				sqlCommand.rollback();
			} catch (SQLException ex) {
				ex.printStackTrace();
			}
			e.printStackTrace();
			log.error("IdtoName() error:" + e.getStackTrace());
		} finally {
			sqlCommand.close();
			sqlCommand = null;
		}
    	return ""; 
    }
    
    /**
	 * 根据Code找名字，国家和联赛用
	 * String oTable 表
	 * String oCode 具体的Code，国家和联赛用
	 * String oName 根据id找的某个字段
	 */
    public String CodetoName(String oTable, String oCode, String oName) throws NoTableException {
		//增加球员信息
		SqlCommand sqlCommand;
		String sql = "select " + oName + " from " + oTable + " where " + "code = '" + oCode + "'";

		sqlCommand = new SqlCommand(sql);
		try {
			ResultSet rs = sqlCommand.executeQuery(); //执行
			
			if(rs.next()){ //如果找到
				return rs.getString(oName);
			}
			
		} catch (SQLException e) {
			try {
				sqlCommand.rollback();
			} catch (SQLException ex) {
				ex.printStackTrace();
			}
			e.printStackTrace();
			log.error("CodetoName() error:" + e.getStackTrace());
		} finally {
			sqlCommand.close();
			sqlCommand = null;
		}
    	return ""; 
    }
    
}